'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview  联盟任务系统
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionMissionConf = require('../../../../config/unionMissionConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService');
var filterSensitiveService = require('../../../../services/filterSensitiveService');
var constantUtil = require('../../../../util/constantUtil.js');
var buildService = require('../../../../services/buildService.js');

/**
 * 领取任务奖励
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var task_id = msg.task_id;

    var union_task_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var unionTask_model = player_manager.unionTask;
                var task_item = unionTask_model.isTaskReceiveTree(task_id);

                if (!task_item) {
                    return next(null, {
                        code: Code.UNION_USER_NOT_UNIONTASK
                    });
                }

                var diff_time = task_item.end_time - moment().unix();
                if (diff_time <= 0) {
                    return next(null, {
                        code: Code.UNION_USER_NOT_UNIONTASK_DIAMOD
                    });
                }

                // 扣除钻石
                var vip_lv = role_model.vip;
                var sub_diamond = buildService.getDirectDiamond(diff_time, vip_lv);
                if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_TASK, sub_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 设置任务时间
                unionTask_model.quickTaskConsume(task_id, diff_time);
                union_task_list = unionTask_model.unionTask_list;
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    union_task_list: union_task_list
                }
            });
        }
    );
};
